package com.ruoyi.web.controller.systembasic;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.SmesProductCategory;
import com.ruoyi.mes.systembasic.service.ISmesProductCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 产品类别Controller
 * 
 * @author yanqin
 * @date 2020-11-26
 */
@RestController
@RequestMapping("/systembasic/productCategory")
public class SmesProductCategoryController extends BaseController
{
    @Autowired
    private ISmesProductCategoryService smesProductCategoryService;

    /**
     * 查询产品类别列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:productCategory:list')")
    @GetMapping("/list")
    public AjaxResult list(SmesProductCategory smesProductCategory)
    {
        List<SmesProductCategory> list = smesProductCategoryService.selectSmesProductCategoryList(smesProductCategory);
        return AjaxResult.success(list);
    }

    /**
     * 导出产品类别列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:productCategory:export')")
    @Log(title = "产品类别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmesProductCategory smesProductCategory)
    {
        List<SmesProductCategory> list = smesProductCategoryService.selectSmesProductCategoryList(smesProductCategory);
        ExcelUtil<SmesProductCategory> util = new ExcelUtil<SmesProductCategory>(SmesProductCategory.class);
        return util.exportExcel(list, "productCategory");
    }

    /**
     * 获取产品类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('systembasic:productCategory:query')")
    @GetMapping(value = "/{productCategoryId}")
    public AjaxResult getInfo(@PathVariable("productCategoryId") Long productCategoryId)
    {
        return AjaxResult.success(smesProductCategoryService.selectSmesProductCategoryById(productCategoryId));
    }

    /**
     * 获取产品类别下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SmesProductCategory smesProductCategory)
    {
        List<SmesProductCategory> list = smesProductCategoryService.selectSmesProductCategoryList(smesProductCategory);;
        return AjaxResult.success(smesProductCategoryService.buildProductCategoryTreeSelect(list));
    }

    /**
     * 新增产品类别
     */
    @PreAuthorize("@ss.hasPermi('systembasic:productCategory:add')")
    @Log(title = "产品类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmesProductCategory smesProductCategory)
    {
        smesProductCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(smesProductCategoryService.insertSmesProductCategory(smesProductCategory));
    }

    /**
     * 修改产品类别
     */
    @PreAuthorize("@ss.hasPermi('systembasic:productCategory:edit')")
    @Log(title = "产品类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmesProductCategory smesProductCategory)
    {
        if(smesProductCategory.getParentId().equals(smesProductCategory.getProductCategoryId())){
            return AjaxResult.error("修改产品类别'" + smesProductCategory.getProductCategoryName() + "'失败，上级类别不能是自己");
        }
        smesProductCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(smesProductCategoryService.updateSmesProductCategory(smesProductCategory));
    }

    /**
     * 删除产品类别
     */
    @PreAuthorize("@ss.hasPermi('systembasic:productCategory:remove')")
    @Log(title = "产品类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productCategoryIds}")
    public AjaxResult remove(@PathVariable Long[] productCategoryIds)
    {
        return toAjax(smesProductCategoryService.deleteSmesProductCategoryByIds(productCategoryIds));
    }
}
